﻿using System;
using UnityEngine;

// Token: 0x02000153 RID: 339
[AddComponentMenu("RVP/C#/Vehicle Controllers/Vehicle Assist", 1)]
[DisallowMultipleComponent]
[RequireComponent(typeof(VehicleParent))]
public class VehicleAssist : MonoBehaviour
{
	// Token: 0x06000616 RID: 1558 RVA: 0x000320DF File Offset: 0x000302DF
	private void Start()
	{
		this.tr = base.transform;
		this.rb = base.GetComponent<Rigidbody>();
		this.vp = base.GetComponent<VehicleParent>();
		this.initialAngularDrag = this.rb.angularDrag;
	}

	// Token: 0x06000617 RID: 1559 RVA: 0x00032118 File Offset: 0x00030318
	private void FixedUpdate()
	{
		if (this.vp.groundedWheels > 0)
		{
			this.groundedFactor = (float)((!this.basedOnWheelsGrounded) ? 1 : (this.vp.groundedWheels / ((!this.vp.hover) ? this.vp.wheels.Length : this.vp.hoverWheels.Length)));
			this.angDragTime = 20f;
			this.rb.angularDrag = this.initialAngularDrag;
			if (this.driftSpinAssist > 0f)
			{
				this.ApplySpinAssist();
			}
			if (this.driftPush > 0f)
			{
				this.ApplyDriftPush();
			}
		}
		else if (this.angularDragOnJump)
		{
			this.angDragTime = Mathf.Max(0f, this.angDragTime - Time.timeScale * TimeMaster.inverseFixedTimeFactor);
			this.rb.angularDrag = ((this.angDragTime <= 0f || (double)this.vp.upDot <= 0.5) ? this.initialAngularDrag : 10f);
		}
		if (this.downforce > 0f)
		{
			this.ApplyDownforce();
		}
		if (this.autoRollOver || this.steerRollOver)
		{
			this.RollOver();
		}
		if (Mathf.Abs(this.vp.localVelocity.y) > this.fallSpeedLimit && (this.vp.localVelocity.y < 0f || this.applyFallLimitUpwards))
		{
			this.rb.AddRelativeForce(Vector3.down * this.vp.localVelocity.y, ForceMode.Acceleration);
		}
	}

	// Token: 0x06000618 RID: 1560 RVA: 0x000322E8 File Offset: 0x000304E8
	private void ApplySpinAssist()
	{
		float num2;
		if (this.autoSteerDrift)
		{
			int num = 0;
			if (this.vp.steerInput != 0f)
			{
				num = (int)Mathf.Sign(this.vp.steerInput);
			}
			this.targetDriftAngle = (((float)num == Mathf.Sign(this.vp.localVelocity.x)) ? ((float)num) : this.vp.steerInput) * -this.maxDriftAngle;
			Vector3 vector = new Vector3(this.vp.localVelocity.x, 0f, this.vp.localVelocity.z);
			Vector3 normalized = vector.normalized;
			Vector3 vector2 = new Vector3(Mathf.Sin(this.targetDriftAngle * 0.017453292f), 0f, Mathf.Cos(this.targetDriftAngle * 0.017453292f));
			Vector3 normalized2 = vector2.normalized;
			Vector3 vector3 = normalized - normalized2;
			num2 = vector3.magnitude * Mathf.Sign(vector3.z) * (float)num * this.driftSpinSpeed - this.vp.localAngularVel.y * Mathf.Clamp01(Vector3.Dot(normalized, normalized2)) * 2f;
		}
		else
		{
			num2 = this.vp.steerInput * this.driftSpinSpeed * ((this.vp.localVelocity.z >= 0f) ? 1f : ((!this.vp.accelAxisIsBrake) ? Mathf.Sign(F.MaxAbs(new float[]
			{
				this.vp.accelInput,
				-this.vp.brakeInput
			})) : Mathf.Sign(this.vp.accelInput)));
		}
		this.rb.AddRelativeTorque(new Vector3(0f, (num2 - this.vp.localAngularVel.y) * this.driftSpinAssist * this.driftSpinCurve.Evaluate(Mathf.Abs(Mathf.Pow(this.vp.localVelocity.x, this.driftSpinExponent))) * this.groundedFactor, 0f), ForceMode.Acceleration);
		float num3 = Vector3.Dot(this.tr.right, this.rb.velocity.normalized);
		if (this.straightenAssist && this.vp.steerInput == 0f && Mathf.Abs(num3) < 0.1f && this.vp.sqrVelMag > 5f)
		{
			this.rb.AddRelativeTorque(new Vector3(0f, num3 * 100f * Mathf.Sign(this.vp.localVelocity.z) * this.driftSpinAssist, 0f), ForceMode.Acceleration);
		}
	}

	// Token: 0x06000619 RID: 1561 RVA: 0x000325C8 File Offset: 0x000307C8
	private void ApplyDownforce()
	{
		if (this.vp.groundedWheels > 0 || this.applyDownforceInAir)
		{
			this.rb.AddRelativeForce(new Vector3(0f, this.downforceCurve.Evaluate(Mathf.Abs(this.vp.localVelocity.z)) * -this.downforce * ((!this.applyDownforceInAir) ? this.groundedFactor : 1f) * ((!this.invertDownforceInReverse) ? 1f : Mathf.Sign(this.vp.localVelocity.z)), 0f), ForceMode.Acceleration);
			if (this.invertDownforceInReverse && this.vp.localVelocity.z < 0f)
			{
				this.rb.AddRelativeTorque(new Vector3(this.downforceCurve.Evaluate(Mathf.Abs(this.vp.localVelocity.z)) * this.downforce * ((!this.applyDownforceInAir) ? this.groundedFactor : 1f), 0f, 0f), ForceMode.Acceleration);
			}
		}
	}

	// Token: 0x0600061A RID: 1562 RVA: 0x00032700 File Offset: 0x00030900
	private void RollOver()
	{
		if (this.vp.groundedWheels == 0 && this.vp.velMag < this.rollSpeedThreshold && (double)this.vp.upDot < 0.8 && this.rollCheckDistance > 0f)
		{
			RaycastHit raycastHit;
			if (Physics.Raycast(this.tr.position, this.vp.upDir, out raycastHit, this.rollCheckDistance, GlobalControl.groundMaskStatic) || Physics.Raycast(this.tr.position, this.vp.rightDir, out raycastHit, this.rollCheckDistance, GlobalControl.groundMaskStatic) || Physics.Raycast(this.tr.position, -this.vp.rightDir, out raycastHit, this.rollCheckDistance, GlobalControl.groundMaskStatic))
			{
				this.rolledOver = true;
			}
			else
			{
				this.rolledOver = false;
			}
		}
		else
		{
			this.rolledOver = false;
		}
		if (this.rolledOver)
		{
			if (this.steerRollOver && this.vp.steerInput != 0f)
			{
				this.rb.AddRelativeTorque(new Vector3(0f, 0f, -this.vp.steerInput * this.rollOverForce), ForceMode.Acceleration);
			}
			else if (this.autoRollOver)
			{
				this.rb.AddRelativeTorque(new Vector3(0f, 0f, -Mathf.Sign(this.vp.rightDot) * this.rollOverForce), ForceMode.Acceleration);
			}
		}
	}

	// Token: 0x0600061B RID: 1563 RVA: 0x000328B4 File Offset: 0x00030AB4
	private void ApplyDriftPush()
	{
		float f = ((!this.vp.accelAxisIsBrake) ? (this.vp.accelInput - this.vp.brakeInput) : this.vp.accelInput) * Mathf.Abs(this.vp.localVelocity.x) * this.driftPush * this.groundedFactor * (1f - Mathf.Abs(Vector3.Dot(this.vp.forwardDir, this.rb.velocity.normalized)));
		this.rb.AddForce(this.vp.norm.TransformDirection(new Vector3(Mathf.Abs(f) * Mathf.Sign(this.vp.localVelocity.x), Mathf.Abs(f) * Mathf.Sign(this.vp.localVelocity.z), 0f)), ForceMode.Acceleration);
	}

	// Token: 0x04000971 RID: 2417
	private Transform tr;

	// Token: 0x04000972 RID: 2418
	private Rigidbody rb;

	// Token: 0x04000973 RID: 2419
	private VehicleParent vp;

	// Token: 0x04000974 RID: 2420
	[Header("Drift")]
	[Tooltip("Variables are multiplied based on the number of wheels grounded out of the total number of wheels")]
	public bool basedOnWheelsGrounded;

	// Token: 0x04000975 RID: 2421
	private float groundedFactor;

	// Token: 0x04000976 RID: 2422
	[Tooltip("How much to assist with spinning while drifting")]
	public float driftSpinAssist;

	// Token: 0x04000977 RID: 2423
	public float driftSpinSpeed;

	// Token: 0x04000978 RID: 2424
	public float driftSpinExponent = 1f;

	// Token: 0x04000979 RID: 2425
	[Tooltip("Automatically adjust drift angle based on steer input magnitude")]
	public bool autoSteerDrift;

	// Token: 0x0400097A RID: 2426
	public float maxDriftAngle = 70f;

	// Token: 0x0400097B RID: 2427
	private float targetDriftAngle;

	// Token: 0x0400097C RID: 2428
	[Tooltip("Adjusts the force based on drift speed, x-axis = speed, y-axis = force")]
	public AnimationCurve driftSpinCurve = AnimationCurve.Linear(0f, 0f, 10f, 1f);

	// Token: 0x0400097D RID: 2429
	[Tooltip("How much to push the vehicle forward while drifting")]
	public float driftPush;

	// Token: 0x0400097E RID: 2430
	[Tooltip("Straighten out the vehicle when sliding slightly")]
	public bool straightenAssist;

	// Token: 0x0400097F RID: 2431
	[Header("Downforce")]
	public float downforce = 1f;

	// Token: 0x04000980 RID: 2432
	public bool invertDownforceInReverse;

	// Token: 0x04000981 RID: 2433
	public bool applyDownforceInAir;

	// Token: 0x04000982 RID: 2434
	[Tooltip("X-axis = speed, y-axis = force")]
	public AnimationCurve downforceCurve = AnimationCurve.Linear(0f, 0f, 20f, 1f);

	// Token: 0x04000983 RID: 2435
	[Header("Roll Over")]
	[Tooltip("Automatically roll over when rolled over")]
	public bool autoRollOver;

	// Token: 0x04000984 RID: 2436
	[Tooltip("Roll over with steer input")]
	public bool steerRollOver;

	// Token: 0x04000985 RID: 2437
	[NonSerialized]
	public bool rolledOver;

	// Token: 0x04000986 RID: 2438
	[Tooltip("Distance to check on sides to see if rolled over")]
	public float rollCheckDistance = 1f;

	// Token: 0x04000987 RID: 2439
	public float rollOverForce = 1f;

	// Token: 0x04000988 RID: 2440
	[Tooltip("Maximum speed at which vehicle can be rolled over with assists")]
	public float rollSpeedThreshold;

	// Token: 0x04000989 RID: 2441
	[Tooltip("Increase angular drag immediately after jumping")]
	[Header("Air")]
	public bool angularDragOnJump;

	// Token: 0x0400098A RID: 2442
	private float initialAngularDrag;

	// Token: 0x0400098B RID: 2443
	private float angDragTime;

	// Token: 0x0400098C RID: 2444
	public float fallSpeedLimit = float.PositiveInfinity;

	// Token: 0x0400098D RID: 2445
	public bool applyFallLimitUpwards;
}
